package com.infoDiffusion.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;

import com.infoDiffusion.bean.UserBean;
import com.infoDiffusion.bean.WeatherInfoBean;
import com.infoDiffusion.service.WeatherInfoService;
import com.infoDiffusion.util.Constants;
import com.infoDiffusion.util.StringUtil;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class WeatherInfoAction extends ActionSupport {
	@Autowired
	WeatherInfoService winfoService;
	WeatherInfoBean winfoBean;
	UserBean bean;
	public HttpServletResponse response = ServletActionContext.getResponse();
	public HttpServletRequest request = ServletActionContext.getRequest();
	public HttpSession session = request.getSession();

	// 添加应急知识
	public String add() {
		bean = (UserBean) request.getSession().getAttribute(Constants.SESSION_ADMIN);
		winfoBean.setUserBean(bean);
		System.out.println(winfoBean);
		boolean flag = winfoService.add(winfoBean);
		if (flag)
			return "addsucc";
		else
			return "addfals";
	}

	// 气象信息列表
	public String list() {
		bean = (UserBean) request.getSession().getAttribute(Constants.SESSION_ADMIN);
		ActionContext.getContext().put("bean", bean);
		List<WeatherInfoBean> list = winfoService.list(winfoBean);
		ActionContext.getContext().put("list", list);
		return "list";
	}

	// 获取ID及bean数据
	public String toUpdate() {
		int id = StringUtil.toInt(request.getParameter("id"));
		if (id > 0) {
			WeatherInfoBean bean = winfoService.getById(id);
			ActionContext.getContext().put("bean", bean);
		}
		return "toUpdate";

	}

	// 更新
	public String update() {
		boolean flag = winfoService.update(winfoBean);
		if (flag)
			return "updateok";
		else
			return "updatefalse";
	}
	// 删除
	public String delete() {
		int id = StringUtil.toInt(request.getParameter("id"));
		if (id > 0) {
			WeatherInfoBean bean = winfoService.getById(id);
			boolean flag = winfoService.delete(bean);
			if (flag)
				return "delete";
		}
		return "flase";
	}




	
	

	public WeatherInfoBean getWinfoBean() {
		return winfoBean;
	}

	public void setWinfoBean(WeatherInfoBean winfoBean) {
		this.winfoBean = winfoBean;
	}

	public UserBean getBean() {
		return bean;
	}

	public void setBean(UserBean bean) {
		this.bean = bean;
	}

}